package protocol

import (
	"context"
	"qld2devservice/proto"
	"qld2devservice/utils"

	"google.golang.org/grpc"
)

func StartChargingSpw(hostUrl string, userId int64, snNum, orderSn string, amount, balance int64) (timeOutFlag bool, openFlag int64, openMsg string, err error) {
	// 连接到 gRPC 服务器
	conn, err := grpc.Dial(hostUrl, grpc.WithInsecure())
	if err != nil {
		errNew := utils.WrapError(err, "StartChargingSpw:1,gRPC服务器连接失败,"+hostUrl)
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return false, 0, "", errNew
	}
	defer conn.Close()

	// 初始化 gRPC 客户端
	client := proto.NewChargingSpwClient(conn)
	res := proto.StartChargingSpwRequest{
		UserID: userId, SnNum: snNum, OrderSn: orderSn, Amount: amount, Balance: balance,
	}
	utils.MyPrint(utils.PRINT_INFO, "StartChargingSpw request", res)
	resp, err := client.StartChargingSpw(context.Background(), &res)
	if err != nil {
		errNew := utils.WrapError(err, "StartChargingSpw:2,gRPC调用失败,"+hostUrl)
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return false, 0, "", errNew
	}
	utils.MyPrint(utils.PRINT_INFO, "StartChargingSpw response", utils.TransProtoToJson(resp))
	return resp.TimeOut, resp.OpenFlag, resp.OpenMsg, nil
}

func StopChargingSpw(hostUrl string, userId int64, snNum string) (timeOutFlag bool, closeFlag int64, amount int64, electricity float32, duration int64, orderSn, recordAmount, closeMsg string, err error) {
	// 连接到 gRPC 服务器
	conn, err := grpc.Dial(hostUrl, grpc.WithInsecure())
	if err != nil {
		errNew := utils.WrapError(err, "StopChargingSpw:1,gRPC服务器连接失败,"+hostUrl)
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return false, 0, 0, 0, 0, "", "", "gRPC服务器连接失败", errNew
	}
	defer conn.Close()

	// 初始化 gRPC 客户端
	client := proto.NewChargingSpwClient(conn)
	res := proto.StopChargingSpwRequest{
		UserID: userId, SnNum: snNum,
	}
	utils.MyPrint(utils.PRINT_INFO, "StopChargingSpw request", res)
	resp, err := client.StopChargingSpw(context.Background(), &res)
	if err != nil {
		errNew := utils.WrapError(err, "StopChargingSpw:2,gRPC调用失败,"+hostUrl)
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return false, 0, 0, 0, 0, "", "", "gRPC调用失败", errNew
	}
	utils.MyPrint(utils.PRINT_INFO, "StopChargingSpw response", utils.TransProtoToJson(resp))
	return resp.TimeOut, resp.CloseFlag, resp.Amount, resp.Electricity, resp.Duration, resp.OrderSn, resp.AmountRecord, resp.CloseMsg, nil
}

func ForceChargingSpw(hostUrl string, userId int64, snNum string) (timeOutFlag bool, closeFlag int64, amount int64, electricity float32, duration int64, orderSn, recordAmount, closeMsg string, err error) {
	// 连接到 gRPC 服务器
	conn, err := grpc.Dial(hostUrl, grpc.WithInsecure())
	if err != nil {
		errNew := utils.WrapError(err, "StopChargingSpw:1,gRPC服务器连接失败,"+hostUrl)
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return false, 0, 0, 0, 0, "", "", "gRPC服务器连接失败", errNew
	}
	defer conn.Close()

	// 初始化 gRPC 客户端
	client := proto.NewChargingSpwClient(conn)
	res := proto.ForceChargingSpwRequest{
		UserID: userId, SnNum: snNum,
	}
	utils.MyPrint(utils.PRINT_INFO, "ForceChargingSpw request", res)
	resp, err := client.ForceChargingSpw(context.Background(), &res)
	if err != nil {
		errNew := utils.WrapError(err, "ForceChargingSpw:2,gRPC调用失败,"+hostUrl)
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return false, 0, 0, 0, 0, "", "", "gRPC调用失败", errNew
	}
	utils.MyPrint(utils.PRINT_INFO, "ForceChargingSpw response", utils.TransProtoToJson(resp))
	return false, 0, resp.Amount, resp.Electricity, resp.Duration, resp.OrderSn, resp.AmountRecord, resp.CloseMsg, nil
}
